﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UserInterfaceShowData.cs" company="Jarno Burger">
//   Jarno Burger
// </copyright>
// <summary>
//   This shows the data on the user interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MyDomotica
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;

    /// <summary>
    /// This shows the data on the user interface.
    /// </summary>
    public class UserInterfaceShowData
    {
        #region Declare
        /// <summary>
        /// The model. This contains the actual data.
        /// </summary>
        private readonly Model.Model model;
        #endregion

        #region Initialize
        /// <summary>
        /// Initializes a new instance of the <see cref="UserInterfaceShowData"/> class.
        /// </summary>
        /// <param name="mainWindow">The main window.</param>
        /// <param name="model">The model.</param>
        public UserInterfaceShowData(MainWindow mainWindow, Model.Model model)
        {
            this.model = model;
            this.model.PropertyChanged += this.OnPropertyChanged;
            this.MainWindow = mainWindow;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the main window.
        /// </summary>
        /// <value>
        /// The main window.
        /// </value>
        public MainWindow MainWindow { get; private set; }
        #endregion

        /// <summary>
        /// Called when [property changed] in model.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // progress-bar
            if (e.PropertyName.Equals("CurrentInternetPushProgress"))
            {
                this.MainWindow.Dispatcher.Invoke(
                                       DispatcherPriority.Normal,
                    (Action)(() => this.MainWindow.ProgressBar.SetPercent(this.model.CurrentInternetPushProgress)));
            }

            // motion
            if (e.PropertyName.Equals("CurrentMotion"))
            {
                this.MainWindow.Dispatcher.Invoke(
                    DispatcherPriority.Normal,
                    (Action)(() => { MainWindow.LabelMovement.Content = this.model.CurrentMotion ? "Yes" : "No"; }));
            }

            // heater
            if (e.PropertyName.Equals("CurrentHeaterIsOn"))
            {
                this.MainWindow.Dispatcher.Invoke(
                    DispatcherPriority.Normal,
                    (Action)(() => { MainWindow.LabelHeating.Content = this.model.CurrentHeaterIsOn ? "On" : "Off"; }));
            }

            // temperature
            if (e.PropertyName.Equals("CurrentTemperature"))
            {
                this.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)this.UpdateTemperatureLabel);
            }

            // preferred temperature
            if (e.PropertyName.Equals("CurrentPreferredTemperature"))
            {
                this.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)this.UpdatePreferredTemperatureLabel);
            }

            // humidity
            if (e.PropertyName.Equals("CurrentHumidity"))
            {
                this.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)this.UpdateTheHumidityLabel);
            }

            // brightness
            if (e.PropertyName.Equals("CurrentBrightness"))
            {
                this.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)this.UpdateTheHelderheidLabel);
            }

            // message
            if (e.PropertyName.Equals("CurrentMessage"))
            {
                this.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)this.ReactToCurrentMessage);
            }

        }

        private void UpdateTemperatureLabel()
        {
            this.MainWindow.LabelCurrentTemperature.Content = this.model.CurrentTemperature;
        }

        /// <summary>
        /// Updates the preferred temperature label.
        /// </summary>
        private void UpdatePreferredTemperatureLabel()
        {
            this.MainWindow.LabelPreferredTemperature.Content = this.model.CurrentPreferredTemperature;
        }

        /// <summary>
        /// Updates the humidity label.
        /// </summary>
        private void UpdateTheHumidityLabel()
        {
            this.MainWindow.LabelVochtigheid.Content = this.model.CurrentHumidity;
        }

        /// <summary>
        /// Updates the helderheid label.
        /// </summary>
        private void UpdateTheHelderheidLabel()
        {
            this.MainWindow.LabelHelderheid.Content = (int)this.model.CurrentBrightness;
        }

        /// <summary>
        /// Reacts to current message. Tries to show it.
        /// </summary>
        private void ReactToCurrentMessage()
        {
            var fadeIn = new DoubleAnimation { From = 0.0, To = 1.0, FillBehavior = FillBehavior.HoldEnd, BeginTime = TimeSpan.FromSeconds(0), Duration = new Duration(TimeSpan.FromSeconds(0.3)) };

            var fadeOut = new DoubleAnimation { From = 1.0, To = 0.0, FillBehavior = FillBehavior.HoldEnd, BeginTime = TimeSpan.FromSeconds(0), Duration = new Duration(TimeSpan.FromSeconds(2)) };

            this.MainWindow.DebugLabel.BeginAnimation(UIElement.OpacityProperty, fadeIn);
            this.MainWindow.DebugLabel.Content = this.model.CurrentMessage;
            this.MainWindow.DebugLabel.BeginAnimation(UIElement.OpacityProperty, fadeOut);
        }

        #region Actions
        /// <summary>
        /// Updates this instance.
        /// </summary>
        public void Update()
        {
            // show time of day
            var time = DateTime.Now;
            var timeOfDayString = string.Format("{0:HH:mm:ss}", time);
            var timeDateString = string.Format("{0:MM-dd-yyyy}", time);
            MainWindow.TimeDay.Content = timeOfDayString;
            MainWindow.TimeDate.Content = timeDateString;
        }
        #endregion
    }
}
